using System;
using System.Text.RegularExpressions;

namespace Lead2012.Utility
{
	/// <summary>
	/// Summary description for Utility.
	/// </summary>
	public class StringFormat
	{
		public static bool IsDate(string str)
		{
			bool isDate = true;
			try
			{
				DateTime dt = DateTime.Parse(str);
			}
			catch
			{
				isDate = false;
			}
			return isDate;
		}

		// Function to Check for AlphaNumeric.
		public static bool IsAlphaNumeric(string str)
		{
            Regex alphaNumeric = new Regex("[^a-zA-Z0-9]");
			return !alphaNumeric.IsMatch(str);    
		}

		public static bool IsAlpha(String str)
		{
			Regex alphaPattern=new Regex("[^a-zA-Z]");
			return !alphaPattern.IsMatch(str);
		}

		public static bool IsNumber(String num)
		{
			Regex notNumPattern=new Regex("[^0-9.-]");
			Regex twoDotPattern=new Regex("[0-9]*[.][0-9]*[.][0-9]*");
			Regex twoMinusPattern=new Regex("[0-9]*[-][0-9]*[-][0-9]*");
			String validRealPattern="^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
			String validIntegerPattern="^([-]|[0-9])[0-9]*$";
			Regex numberPattern =new Regex("(" + validRealPattern +")|(" + validIntegerPattern + ")");
			return !notNumPattern.IsMatch(num) &&
				!twoDotPattern.IsMatch(num) &&
				!twoMinusPattern.IsMatch(num) &&
				numberPattern.IsMatch(num);
		} 

		public static string ConvertDecimal(decimal Number, int DecimalPlaces)
		{
			//Added by OBrown. There is a need to format #'s without Rounding
			string strReturn = Number.ToString();

			int nDecimalIndex = strReturn.IndexOf(".");
			if (nDecimalIndex > 0)
			{
				strReturn = strReturn.Substring(0, ((nDecimalIndex + 1) + DecimalPlaces));
			}
			else
			{
				strReturn = strReturn + ".00";
			}

			return strReturn;
		}
		
		public static bool IsEmpty(object val)
		{
			try
			{
				if (val == null) {return true;}
				if (val == System.DBNull.Value) {return true;}
				if (val.GetType() == typeof(string))
				//if (val.GetType() == System.Type.GetType("string")) 
				{
					if (Convert.ToString(val).Trim().Length ==0)
					{return true;}
				}
			}
			catch(Exception)
			{
				return true;
			}

			return false;
		}

		public static object Nvle(object x, object y)
		{
			if (IsEmpty(x)) {return y;} 
			else {return x;}
		}

		public static string CNullString(object o)
		{
			if (o == null) {return "";}
			return o.ToString();
		}

		public static string FormatArrayString(System.Collections.ArrayList arr)
		{
			string val = string.Empty;
			if (arr.Count > 0)
			{
				for (int i = 0; i < arr.Count; i++)
				{
					val += arr[i].ToString() + " ";
				}
			}
			return val.Trim();
		}

		public static string FormatArrayString(System.Collections.ArrayList arr, string delimiter)
		{
			string val = string.Empty;
			if (arr.Count > 0)
			{
				for (int i = 0; i < arr.Count; i++)
				{
					val += arr[i].ToString() + delimiter;
				}
			}
			if (val.Length > 0) {val = val.Remove(val.Length-1,1);}
			return val;
		}

		public static string FormatArrayString(System.Collections.ArrayList arr, int index)
		{
			string val = string.Empty;
			if (arr.Count > 0)
			{
				for (int i = 0; i < arr.Count; i++)
				{
					if (i == index)
					{
						//HACK: Why is delimiter still here for 0 index items?
						if (arr[i].ToString().IndexOf("|") > 0)
						{
							val = arr[i].ToString().Substring(0,arr[i].ToString().IndexOf("|"));
						}
						else
						{
							val = arr[i].ToString();
						}
						return val.Trim();
					}
					//val += arr[i].ToString() + " ";
				}
			}
			return val.Trim();
		}

		public static string FormatStringForSql(string s)
		{
			return FormatSQL(s.Replace("'","''"));
		}

		public static string FormatArrayForSql(string[] s)
		{
			System.Text.StringBuilder oBuilder = new System.Text.StringBuilder();
			for (int i=0; i <= s.GetUpperBound(0); i++)
			{
				oBuilder.Append(FormatStringForSql(s.GetValue(i).ToString()));
				oBuilder.Append(",");
			}
			if (oBuilder.ToString().Length > 0) {oBuilder.Remove(oBuilder.Length-1,1);}
			return oBuilder.ToString();
		}

		public static string CleanBadWords(string sWords)
		{
			string sBadWord="insert,drop,truncate,delete,;,xp_cmd";
			char[] sep={','};
			Array aBadWord= sBadWord.Split(sep);
			
			for (int counter =0; counter <= aBadWord.GetUpperBound(0); counter++) 
			{
				sWords=sWords.Replace(aBadWord.GetValue(counter).ToString(),"");
			}
			return sWords;
		}

		public static string CleanBadWords(string sWords, bool bFormatSql)
		{
			sWords=CleanBadWords(sWords);
			if (bFormatSql==true)
			{
				return FormatSQL(sWords);
			}
			else
			{
				return sWords;
			}
		}
	
		public static string FormatSQL(string sWords)
		{
			return "'" + sWords + "'";		
		}	

		public static string Left(string EvalString, int length)
		{
			string tmpstr = EvalString.Substring(0, length);
			return tmpstr;
		} 

		public static string Right(string EvalString, int length)
		{
			string tmpstr = EvalString.Substring(EvalString.Length-length,length);
			return tmpstr;
		}

        public static string FormatNumber(int x)
        {
            if (x.ToString().Equals("1") || x.ToString().IndexOf("1", x.ToString().Length - 1, 1) > 0)
            { return x.ToString() + "st"; }
            if (x.ToString().Equals("2") || x.ToString().IndexOf("2", x.ToString().Length - 1, 1) > 0)
            { return x.ToString() + "nd"; }
            if (x.ToString().Equals("3") || x.ToString().IndexOf("3", x.ToString().Length - 1, 1) > 0)
            { return x.ToString() + "rd"; }

            return x.ToString() + "th";
        }
	}
}